package DMSXL._2024.BinaryTrees;

public class insertIntoBST {
    public static void main(String[] args) {
        Integer[] nums = {55,28,92,26,43,null,null,null,null,null,null};
        TreeNode root = CreateTree.createBinaryTree(nums,0);
        new insertIntoBST().insertIntoBST(root,1);
    }
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root == null){
            return new TreeNode(val);
        }
        boolean insert = true;
        TreeNode cur = root;
        while (insert){
            while (val < cur.val){
                if(cur.left != null){
                    cur = cur.left;
                }else{
                    break;
                }
            }
            if(val > cur.val && cur.right == null){
                cur.right = new TreeNode(val);
                insert = false;
            }
            while (val > cur.val){
                if(cur.right != null){
                    cur = cur.right;
                }else {
                    break;
                }
            }
            if(val < cur.val && cur.left == null){
                cur.left = new TreeNode(val);
                insert = false;
            }
        }
        return root;
    }
}
